import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */
/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard', affix: true }
    }]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/org',
    component: Layout,
    redirect: '/org/sysUser',
    name: 'Org',
    meta: { title: '组织架构', icon: 'el-icon-s-home', roles: ['SYS_ORG'] },
    children: [
      {
        path: 'sysCompany',
        name: 'SysCompany',
        component: () => import('@/views/sysCompany/index'),
        meta: { title: '公司管理', icon: 'company', roles: ['SYS_COMPANY'] }
      },
      {
        path: 'sysUser',
        name: 'SysUser',
        component: () => import('@/views/sysUser/index'),
        meta: { title: '用户管理', icon: 'user', roles: ['SYS_USER'] }
      },
      {
        path: 'sysRole',
        name: 'SysRole',
        component: () => import('@/views/sysRole/index'),
        meta: { title: '角色管理', icon: 'role', roles: ['SYS_ROLE'] }
      },
      {
        path: 'sysPermission',
        name: 'SysPermission',
        component: () => import('@/views/sysPermission/index'),
        meta: { title: '权限管理', icon: 'menu', roles: ['SYS_PERMISSION'] }
      },
      {
        path: 'sysData',
        name: 'SysData',
        component: () => import('@/views/sysData/index'),
        meta: { title: '数据导入', icon: 'data', roles: ['SYS_DATA'] }
      }
    ]
  },
  {
    path: '/setting',
    component: Layout,
    redirect: '/setting/sysJob',
    name: 'Setting',
    meta: { title: '系统设置', icon: 'el-icon-setting', roles: ['SYS_SETTING'] },
    children: [
      {
        path: 'sysJob',
        name: 'SysJob',
        component: () => import('@/views/sysJob/index'),
        meta: { title: '定时任务', icon: 'job', roles: ['SYS_JOB'] }
      },
      {
        path: 'sysMonitor',
        name: 'SysMonitor',
        component: () => import('@/views/sysMonitor/index'),
        meta: { title: '运行监控', icon: 'monitor', roles: ['SYS_MONITOR'] }
      },
      {
        path: 'sysConfig',
        name: 'SysConfig',
        component: () => import('@/views/sysConfig/index'),
        meta: { title: '配置管理', icon: 'setting', roles: ['SYS_CONFIG'] }
      },
      {
        path: 'sysLog',
        name: 'SysLog',
        component: () => import('@/views/sysLog/index'),
        meta: { title: '系统日志', icon: 'log', roles: ['SYS_LOG'] }
      },
      {
        path: 'sysSms',
        name: 'SysSms',
        component: () => import('@/views/sysSms/index'),
        meta: { title: '短信管理', icon: 'sms', roles: ['SYS_SMS'] }
      },
      {
        path: 'sysEmail',
        name: 'SysEmail',
        component: () => import('@/views/sysEmail/index'),
        meta: { title: '邮件管理', icon: 'email', roles: ['SYS_EMAIL'] }
      },
      {
        path: 'sysTemplate',
        name: 'SysTemplate',
        component: () => import('@/views/sysTemplate/index'),
        meta: { title: '模板设置', icon: 'form', roles: ['SYS_TEMPLATE'] }
      },
      {
        path: 'sysBanner',
        name: 'SysBanner',
        component: () => import('@/views/sysBanner/index'),
        meta: { title: '滚动信息', icon: 'banner', roles: ['SYS_BANNER'] }
      },
      {
        path: 'sysQueryMain',
        name: 'SysQueryMain',
        component: () => import('@/views/sysQueryMain/index'),
        meta: { title: '高级查询', icon: 'query', roles: ['SYS_QUERY_MAIN'] }
      }
    ]
  },
  {
    path: '/integration',
    component: Layout,
    redirect: '/integration/sysPay',
    name: 'Integration',
    meta: { title: '集成设置', icon: 'el-icon-s-ticket', roles: ['SYS_INTEGRATION'] },
    children: [
      {
        path: 'sysPay',
        name: 'SysPay',
        component: () => import('@/views/sysPay/index'),
        meta: { title: '支付管理', icon: 'order', roles: ['SYS_PAY'] }
      },
      {
        path: 'sysInvoice',
        name: 'SysInvoice',
        component: () => import('@/views/sysInvoice/index'),
        meta: { title: '发票管理', icon: 'ticket', roles: ['SYS_INVOICE'] }
      },
      {
        path: 'sysNuoInvoice',
        name: 'SysNuoInvoice',
        component: () => import('@/views/sysNuoInvoice/index'),
        meta: { title: '诺诺发票', icon: 'invoice', roles: ['SYS_NUO_INVOICE'] }
      },
      {
        path: 'sysExpress',
        name: 'SysExpress',
        component: () => import('@/views/sysExpress/index'),
        meta: { title: '顺丰快递', icon: 'sysExpress', roles: ['SYS_EXPRESS'] }
      },
      {
        path: 'sysDataxConfig',
        name: 'SysDataxConfig',
        component: () => import('@/views/sysDataxConfig/index'),
        meta: { title: '数据同步', icon: 'datax', roles: ['SYS_DATAX_CONFIG'] }
      }
    ]
  },
  {
    path: '/category',
    component: Layout,
    redirect: '/category/sysMajorCategory',
    name: 'Category',
    meta: { title: '分类管理', icon: 'el-icon-s-grid', roles: ['SYS_CATEGORY'] },
    children: [
      {
        path: 'sysMajorCategory',
        name: 'SysMajorCategory',
        component: () => import('@/views/sysMajorCategory/index'),
        meta: { title: '专业分类', icon: 'classification', roles: ['SYS_MAJOR_CATEGORY'] }
      },
      {
        path: 'sysNewsCategory',
        name: 'SysNewsCategory',
        component: () => import('@/views/sysNewsCategory/index'),
        meta: { title: '公告分类', icon: 'el-icon-folder-opened' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'hash', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
